package ai.zixing.strategy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@RefreshScope
public class PlanStrategyContext {

    /**
     * 将具体策略存放到 strategyMap
     */
    public static Map<String, APlanStrategy> strategyMap = new HashMap<>();

    /**
     * 将具体策略存放到 strategyMap 中
     * 策略抽象类中调用
     *
     * @param strategy strategy
     */
    public void register(APlanStrategy strategy) {
        strategyMap.put(strategy.getStrategyName(), strategy);
    }

    /**
     * nacos 中读取需要的具体策略
     */
    @Value("${planName}")
    private String planName;

    /**
     * 根据 nacos 配置动态获取 bean
     * 可以根据业务来获具体的策略 bean
     *
     * @return APlanStrategy
     */
    public APlanStrategy getOneStrategy() {
        return strategyMap.get(planName);
    }
}
